package com.bingkaimedia.dompetobeta.app;
 
  
 

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bingkaimedia.dompetobeta.R;
import com.bingkaimedia.dompetobeta.libs.AlertDialogManager;
import com.bingkaimedia.dompetobeta.libs.JSONfunctions;
import com.bingkaimedia.dompetobeta.libs.LazyAdapter2;
import com.bingkaimedia.dompetobeta.libs.SessionManager;
import com.bingkaimedia.dompetobeta.libs.parser_Json;
import com.bingkaimedia.dompetobeta.libs_image.ImageLoader;
import com.bingkaimedia.dompetobeta.libs_slidemenu.BaseActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable; 
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.ParseException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message; 
import android.telephony.TelephonyManager;
import android.text.Editable; 
import android.text.TextWatcher;
import android.util.DisplayMetrics; 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView; 
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
  

@SuppressLint("NewApi")
public class Forgot_pass  extends Activity  {
	 
	int current_page = 0;
	 
	// XML node keys 
	//static final String KEY_SONG = "song"; // parent node
	
	public static final String KEY_COUNTRIES_ID = "id_countries";  
	public static final String KEY_COUNTRIES_CODE = "countryCode";  
	public static final String KEY_COUNTRIES_NAME = "countryName";  
	public static final String KEY_COUNTRIES_PHONE = "countryPhone";  
	
	
	public static final String KEY_VOUCHER_ID = "voucher_id";
	public static final String KEY_VOUCHER_NAME = "voucher_name"; 
	public static final String KEY_VOUCHER_TOTAL = "voucher_total";
	public static final String KEY_VOUCHER_TERSEDIA = "voucher_tersedia";
	public static final String KEY_VOUCHER_DETAIL = "voucher_detail";
	public static final String KEY_VOUCHER_DATE = "voucher_date";
	public static final String KEY_VOUCHER_DESCRIPTION = "voucher_description";
	public static final String KEY_MERCHANT_ID = "merchant_id";
	public static final String KEY_MERCHANT_NAME = "merchant_name";
	public static final String KEY_MERCHANT_CATEGORY = "merchant_category";
	public static final String KEY_MERCHANT_PHOTO = "merchant_photo";
	public static final String KEY_TRANSACTION_ID = "transaction_id";
	public static final String KEY_TRANSACTION_DATE = "transaction_date";
	public static final String KEY_TRANSACTION_IMAGE = "transaction_image";
	public static final String KEY_TRANSACTION_STATUS = "transaction_status";
	
	public static final String KEY_MY_CHANNEL_ID = "my_channel_id";
	public static final String KEY_CHANNEL_ID = "channel_id";
	public static final String KEY_USERS_ID = "users_id";
	public static final String KEY_CHANNEL_NAME = "channel_name";
	public static final String KEY_CHANNEL_ADDRESS = "channel_address";
	public static final String KEY_CHANNEL_PHOTO = "channel_photo";
	public static final String KEY_CHANNEL_NOTE = "channel_note";
	
	 
	
	
	private String xml_countries_id;
	private String xml_countries_code;
	private String xml_countries_name;
	private String xml_countries_phone;
	 
	
	private String session_userid;
	private String session_username;
	private String session_password;
	private String session_email;
	private String session_city;
	private String session_checked;
	private String session_channel;
	private String session_category;
    

	private int count;
	private String json; 
	private JSONObject jsonobject;
	public String code;
	public String msg;
    
    public ImageLoader imageLoader; 
    
    private String idtransaksi;
    

	private AutoCompleteTextView autoComplete;
	private AutoCompleteTextView autoComplete2;
	 
 // Session Manager Class
 	SessionManager session;
 	
 	LazyAdapter2 adapter;
 	ArrayList<HashMap<String, String>> menuItems;
 	ProgressDialog pDialog;
 	HashMap<String, String> map = new HashMap<String, String>();
 	
 	ArrayList<HashMap<String, String>> diskonba_list = new ArrayList<HashMap<String, String>>();
 	

	public int i = 0;
 	//slide menu
 	private LinearLayout slidingPanel;
 	private boolean isExpanded;
 	private DisplayMetrics metrics;
 	private RelativeLayout headerPanel;
 	private int panelWidth;
 	private int panelWidth1;

 	private String sta="";
 	private String id_;
 	ArrayList nameValuePairs;
	String result = null;
	InputStream is = null;
	StringBuilder sb=null;
	ImageLoader imgLoader;
	String tanggal1,tanggal2;
 	private ImageView menuViewButton,menuRightButton;

 	FrameLayout.LayoutParams menuPanelParameters;
 	FrameLayout.LayoutParams slidingPanelParameters;
 	LinearLayout.LayoutParams headerPanelParameters;
 	LinearLayout.LayoutParams listViewParameters;
 	
 	private TextView lbl_forgot_judul;
 	private TextView lbl_forgot_detail;
 	private EditText txt_forgot_email;
	private Button cmd_forgot_submit;   

	private Boolean btxt_forgot_email;
	
	private ArrayList<Countries_Array> countrieslist;
	private ArrayList<Countries_Array> countrieslist2;
	final List<String> list = new ArrayList<String>();
	final List<String> list2 = new ArrayList<String>();
	
	public static double lat;
	public static double lng;
	
	Drawable errorIcon;
	

	AlertDialogManager alert = new AlertDialogManager();
 
	/*
	@Override
	public void onBackPressed() { 
		//overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		
		Toast.makeText(getApplicationContext(), "back",
				3000).show();
		Register.this.finish();
	}
	*/
	
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		Toast.makeText(getApplicationContext(), "back",
				3000).show();
		//Register.this.finish();
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        setContentView(R.layout.forgot_pass);
         
        inisialisasi();	
        form_error();
        form_click();
        session();

		
         
        
    }
	
	
	private void inisialisasi(){ 
		
		
		lbl_forgot_judul = (TextView) findViewById(R.id.lbl_forgot_judul); 
		lbl_forgot_detail = (TextView) findViewById(R.id.lbl_forgot_detail); 
        txt_forgot_email = (EditText) findViewById(R.id.txt_forgot_email); 
        cmd_forgot_submit = (Button) findViewById(R.id.cmd_forgot_submit); 
         

       
        Typeface font = Typeface.createFromAsset(getAssets(), "gisha.ttf");  
         
        lbl_forgot_judul.setTypeface(font);
        lbl_forgot_detail.setTypeface(font);
        txt_forgot_email.setTypeface(font);
        cmd_forgot_submit.setTypeface(font);
		
        ImageView diskonion_iconmenu = (ImageView) findViewById(R.id.diskonion_iconmenu); 

		diskonion_iconmenu.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
	 				Intent intent=new Intent(getApplicationContext(),Signin.class);
	 				startActivity(intent); 
                    finish();
			}
		});
	}
	
	private void session(){
		
		//ngurusin session
		session = new SessionManager(getApplicationContext());
	    
	    HashMap<String, String> user = session.getUserDetails();
	     
	    // name
	    session_userid = user.get(SessionManager.id_users);
	    session_username = user.get(SessionManager.username);
	    session_password = user.get(SessionManager.password);
	    session_email = user.get(SessionManager.email);
	    session_city = user.get(SessionManager.city);
	    session_checked = user.get(SessionManager.checked);
	    session_channel = user.get(SessionManager.channel);
	    session_category = user.get(SessionManager.category);
		
	    // getting intent data
        Intent in = getIntent();
         
        // Get XML values from previous intent
        id_ = in.getStringExtra(KEY_CHANNEL_ID);
        //String title_ = in.getStringExtra(KEY_MERCHANT_NAME);
        //String artist_ = in.getStringExtra(KEY_VOUCHER_NAME);
        //String duration_ = in.getStringExtra(KEY_VOUCHER_TOTAL);
        //String thumb_url_ = in.getStringExtra(KEY_MERCHANT_PHOTO);
        // new loadListView().execute();
	}
	 
	private void form_error(){
		 
		txt_forgot_email.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				 
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (txt_forgot_email.getText().toString() == null) {
					txt_forgot_email.requestFocus();
					txt_forgot_email.setError("Insert right email!");

					btxt_forgot_email = false; 
					
				} else if (isEmailValid(txt_forgot_email.getText().toString()) == false) {
					txt_forgot_email.requestFocus();
					txt_forgot_email.setError("Insert right email!");
					
					btxt_forgot_email = false; 
					 
				} else {
					btxt_forgot_email = true;
					if((btxt_forgot_email=true))
					{ 
					}
					else
					{ 
					}
				}
			}
		});
		
		 
		 
	}
	
	public void form_click(){
		 
		 

		cmd_forgot_submit.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) 
				{
					    
					//System.out.println("click");
					new Send().execute();
				}
		});
		
		 
	}
	
	 
	 
	  
	@SuppressLint("NewApi")
	class Send extends AsyncTask {

		private String json;
		private JSONObject jsonobject;
		private String code;
		private String msg;
		public String url2;

		@Override
		protected void onPreExecute() {
			super.onPreExecute(); 
			pDialog = new ProgressDialog(Forgot_pass.this);
			pDialog.setMessage("Please wait...");
			pDialog.setCancelable(true);
			pDialog.show();
			
			url2 = Global.link_forgot_pass + 
					"mail="+txt_forgot_email.getText().toString();
			
			 
			url2 = url2.replace(" ", "%20"); 
			System.out.println(url2);

		}

		@Override
		protected Object doInBackground(Object... params) {
			code="99";
			msg="Not connected to server";
			
			try {
				json = JSONfunctions.getStringContent(url2);
				jsonobject = new JSONObject(json);
				code = jsonobject.getString("code");
				//code = "1";
				msg = jsonobject.getString("msg");
				//Toast.makeText(getApplicationContext(), "Mohon Tunggu",
				//		30000).show();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			if (code.equalsIgnoreCase("1")) {
				//alert.showAlertDialog(Register.this, "Notification", "Registration success, check your email for activation.", true);
				
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(Forgot_pass.this); 
	            alertDialog.setTitle("Notification"); 
	            alertDialog.setMessage(msg); 
	            alertDialog.setIcon(R.drawable.image_success); 
	            alertDialog.setPositiveButton("OK",
	                    new DialogInterface.OnClickListener() {
	                        public void onClick(DialogInterface dialog,int which) {
	                            // Write your code here to execute after dialog
	                        	Intent i = new Intent(getApplicationContext(), Signin.class);
	                            startActivity(i); 
	                            finish();
	                        }
	                    });
	            

	            // Showing Alert Message
	            alertDialog.show();
				   
			} else {
				//Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
				//alert.showAlertDialog(Register.this, "Notification", msg, false);
				
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(Forgot_pass.this); 
	            alertDialog.setTitle("Notification"); 
	            alertDialog.setMessage(msg); 
	            alertDialog.setIcon(R.drawable.image_fail); 
	            alertDialog.setPositiveButton("OK",
	                    new DialogInterface.OnClickListener() {
	                        public void onClick(DialogInterface dialog,int which) {
	                            // Write your code here to execute after dialog
	                        	Intent i = new Intent(getApplicationContext(), Signin.class);
	                            startActivity(i); 
	                            finish();
	                        }
	                    });
	            

	            // Showing Alert Message
	            alertDialog.show();
			}
			

			if (pDialog.isShowing())
				pDialog.dismiss();
			 
			
		}

	}
	
	boolean isEmailValid(CharSequence email) {
		return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
	} // end of email matcher


	
	 
	
	public boolean isonline()
	{
		ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni=cm.getActiveNetworkInfo();
		if(ni != null && ni.isConnected())
		{
			return true;
		}
		return false;
		
	}
	 
	 
}
